package com.study.bigdata.spark.core.rdd.operator.transform

import org.apache.spark.{SparkConf, SparkContext}

object Scala16_RDD_Operator_Transform_1{
  def main(args: Array[String]): Unit = {

    val conf = new SparkConf().setMaster("local[*]").setAppName("RDD")
    conf.set("spark.local.dir","D:\\hadoopbook\\spark\\test")
    val sc = new SparkContext(conf)
    // TODO 算子 - 转换 -  aggregateByKey
    val rdd = sc.makeRDD(
      List(
        ("a", 1), ("a", 2), ("b", 3),
        ("b", 4), ("b", 5), ("a", 6)
      ), 2
    )
    // TODO aggregateByKey也可以实现WorldCount 4
    val rdd1 = rdd.aggregateByKey(0)(_+_,_+_)
    rdd1.collect().foreach(println)
    /*
    (b,12)
    (a,9)
     */
    // TODO foldByKey也可以实现WorldCount 5
    // TODO 如果aggregateByKey算子分区内计算逻辑和分区外一致，那么就可以使用foldByKey简化
    val rdd2 = rdd.foldByKey(0)(_+_)
    rdd2.collect().foreach(println)
    /*
    (b,12)
    (a,9)
     */
    sc.stop()

  }

}
